import greenfoot.*;

/**
 * This class defines a crab. Crabs live on the beach.
 */
public class Crab extends Actor
{
    GreenfootImage image1 = new GreenfootImage("crab.png");
    GreenfootImage image2 = new GreenfootImage("crab2.png");
    int count = 0;
    public Crab() {
        setImage(image1);
    }
    
    public void act() {
        switchImage();
        keyControy();
        eat();
        panduan();
    }
    
    public void switchImage() {
        if (getImage() == image1)
            setImage(image2);
        else
            setImage(image1);
    }
    
    public void eat()
    {
        if (isTouching​(Worm.class)) {
            removeTouching​(Worm.class);
            Greenfoot.playSound("slurp.wav");
            count++;
            CrabWorld w = (CrabWorld)getWorld();
            //w.showText("吃虫数：" + count, 45, 30);
            w.showScore(count);
        }
        
    }
    
    public void panduan()
    {
        if (count == 3) {
            World w = getWorld();
            GreenfootImage bg = w.getBackground();
            bg.setFont(new Font(true,true,150));
            bg.setColor(new Color(0,255,0));
            bg.drawString("成功！" ,200, 300);
            Greenfoot.stop();
        }
    }
    
    public void keyControy()
    {
       if (Greenfoot.isKeyDown("w")) {
            move(4);
       }
       if (Greenfoot.isKeyDown("left")) {
            turn(-3);
       }
       if (Greenfoot.isKeyDown("right")) {
            turn(3);
       } 
       if (Greenfoot.isKeyDown("e")) {
            move(10);
       }
    }
}


